package com.chillis.standings.domain.table;

import com.chillis.standings.domain.Stage;

import java.util.ArrayList;
import java.util.List;

public class TablesFactory {
   public static Table create(TableSettings.Format format, int participantsCount) {
      if (format == TableSettings.Format.Round) {
         return createRoundTable(participantsCount);
      } else if (format == TableSettings.Format.Olympic) {
         return createOlympic(participantsCount);
      } else {
         throw new Error("can't create table because format is empty or unknown");
      }
   }

   public static Table createRoundTable(int participantsCount) {
      List<Stage> stages = new ArrayList<Stage>();

      for (int i = 1; i < participantsCount; ++i) {
         stages.add(new Stage(participantsCount / 2));
      }

      return new Table(stages);
   }

   public static Table createOlympic(int participantsCount) {
      List<Stage> stages = new ArrayList<Stage>();
      int stageParticipantsCount = participantsCount;

      while (stageParticipantsCount > 1) {

         if (stageParticipantsCount % 2 > 0) throw new Error("participants count could be divided by 2");

         stageParticipantsCount /= 2;

         stages.add(new Stage(stageParticipantsCount));
      }

      return new Table(stages);
   }

}
